﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

using System.Text;
namespace Golf.WebUI.AdminShow.CommoditiesMmanagement
{
    public partial class Golf_Product_Img : System.Web.UI.Page
    {
        public DataTable dt = new DataTable();
        /// <summary>
        /// 商品id
        /// </summary>
        public int _Pro_ID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("Id", typeof(string));
            dt.Columns.Add("Pro_Picture", typeof(string));
            dt.Columns.Add("unit", typeof(string));
            if (!Page.IsPostBack)
            {
                switch (Request.Params["action"])
                {
                    case "modifyImg"://修改
                        GetPicture();
                        break;
                    case "ajax_del_img":
                        ajax_del_img();
                        break;
                }
            }
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        private void ajax_del_img()
        {
            string[] Picture = { };
            if (Request.Params["imgs"] != null && Request.Params["imgs"].Trim() != "")
            {
                Picture = Request.Params["imgs"].Split(',');
            }
            int id = 0;
            if (Request.Params["_id"] != null && Request.Params["_id"].Trim() != "")
            {
                id = (Convert.ToInt32(Request.Params["_id"]));
            }
            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            Golf.Model.Golf_Product model = bll.GetModel(id);
            string[] _Picture = { };
            string[] __Picture = { };
            if (model == null)
                return;
            _Picture = model.Pro_Picture.Split(',');
            __Picture = model.Pro_Picture.Split(',');
            for (int i = 0; i < _Picture.Length; i++)
            {
                foreach (var item in Picture)
                {
                    if (_Picture[i].Equals(item))
                    {
                        __Picture[i] = null;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            StringBuilder big = new StringBuilder();
            StringBuilder small = new StringBuilder();
            #region 删除图片  网站上的图片
            foreach (var item in Picture)
            {
                if (item == null) continue;
                int i = item.LastIndexOf(".");
                string unit = item.Substring(i);
                string imgSrc = item.Substring(0, i);
                string imgSrc_small = imgSrc + "_small" + unit;
                string imgSrc_big = imgSrc + "_big" + unit;
                sb.Append(item + ",");
                big.Append(imgSrc_big + ",");
                small.Append(imgSrc_small + ",");
                string Path_Temp = Server.MapPath("~") + "/ImageProductsTemp/" + item;
                string Path = Server.MapPath("~") + "/ImageProducts/" + item;
                string Path_small = Server.MapPath("~") + "/ImageProducts/" + imgSrc_small;
                string Path_big = Server.MapPath("~") + "/ImageProducts/" + imgSrc_big;
                try
                {
                    FileInfo fi_Temp = new FileInfo(Path_Temp);
                    if (File.Exists(Path_Temp))
                    {
                        //删除
                        fi_Temp.Delete();
                    }

                    FileInfo fi = new FileInfo(Path);
                    if (File.Exists(Path))
                    {
                        //删除
                        fi.Delete();
                    }
                    FileInfo fi_mall = new FileInfo(Path_small);
                    if (File.Exists(Path_small))
                    {
                        //删除
                        fi_mall.Delete();
                    }
                    FileInfo fi_big = new FileInfo(Path_big);
                    if (File.Exists(Path_big))
                    {
                        //删除
                        fi_big.Delete();
                    }
                }
                catch (Exception)
                {

                }
            }
            #endregion
            sb = new StringBuilder();
            big = new StringBuilder();
            small = new StringBuilder();
            foreach (var item in __Picture)
            {
                if (item == null) continue;
                int i = item.LastIndexOf(".");
                string unit = item.Substring(i);
                string imgSrc = item.Substring(0, i);
                string imgSrc_small = imgSrc + "_small" + unit;
                string imgSrc_big = imgSrc + "_big" + unit;
                sb.Append(item + ",");
                big.Append(imgSrc_big + ",");
                small.Append(imgSrc_small + ",");
            }
            if (sb.Length > 0 && big.Length > 0 && small.Length > 0)
            {
                model.Pro_Picture = sb.ToString().Substring(0, sb.Length - 1);
                model.Pro_PicSmall = big.ToString().Substring(0, big.Length - 1);
                model.Pro_PicBig = small.ToString().Substring(0, small.Length - 1);

            }
            else
            {
                model.Pro_Picture = "";
                model.Pro_PicSmall = "";
                model.Pro_PicBig = "";
            }
            if (bll.Update(model))
            {
                Response.Write("0|");
            }
            else
            {
                Response.Write("1|");
            }

        }
        /// <summary>
        /// 获取商品图片
        /// </summary>
        public void GetPicture()
        {
            int id = 0;
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                id = (Convert.ToInt32(Request.Params["id"]));
                _Pro_ID = id;
                labID.Text = id + "";
            }
            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            Golf.Model.Golf_Product model = bll.GetModel(id);
            string[] Picture;
            if (model == null)
                return;
            if (model.Pro_Picture == null || model.Pro_Picture == "")
                return;
            Picture = model.Pro_Picture.Split(',');
            foreach (var item in Picture)
            {
                DataRow dr = dt.NewRow();
                dr["Id"] = model.Id;
                dr["Pro_Picture"] = item;
                int i = item.LastIndexOf(".");
                string unit = item.Substring(0, i);
                dr["unit"] = unit;
                dt.Rows.Add(dr);
            }
            dt.Dispose();
        }

        protected void btn_addImg_Click(object sender, EventArgs e)
        {
            //默认图片
            string Pro_Picture = "";
            string Pro_PicSmall = "";
            string Pro_PicBig = "";
            StringBuilder sb_Pro_Picture = new StringBuilder();
            StringBuilder sb_Pro_PicSmall = new StringBuilder();
            StringBuilder sb_Pro_PicBig = new StringBuilder();

            string[] picture = hid_img_ifram.Value.Split('|');//获取传入的文件名
            foreach (var item in picture)
            {
                string[] p_one = item.Split(',');
                sb_Pro_Picture.Append(p_one[0] + ",");
                sb_Pro_PicSmall.Append(p_one[1] + ",");
                sb_Pro_PicBig.Append(p_one[2] + ",");
            }
            if (sb_Pro_Picture.Length != 0 && sb_Pro_PicSmall.Length != 0 && sb_Pro_PicBig.Length != 0)
            {
                Pro_Picture = sb_Pro_Picture.ToString().Substring(0, sb_Pro_Picture.Length - 1);
                Pro_PicSmall = sb_Pro_PicSmall.ToString().Substring(0, sb_Pro_PicSmall.Length - 1);
                Pro_PicBig = sb_Pro_PicBig.ToString().Substring(0, sb_Pro_PicBig.Length - 1);
            }

            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            string labid = labID.Text;
            int id = 0;
            try
            {
                id = int.Parse(labid);
            }
            catch (Exception)
            {
            }

            Golf.Model.Golf_Product model = bll.GetModel(id);
            if (model == null)
            {
                return;
            }
            else
            {
                if (model.Pro_Picture == "" && model.Pro_PicSmall == "" && model.Pro_PicBig == "")
                {
                    model.Pro_Picture = Pro_Picture;
                    model.Pro_PicSmall = Pro_PicSmall;
                    model.Pro_PicBig = Pro_PicBig;
                }
                else if (model.Pro_Picture == null && model.Pro_PicSmall == null && model.Pro_PicBig == null)
                {
                    model.Pro_Picture = Pro_Picture;
                    model.Pro_PicSmall = Pro_PicSmall;
                    model.Pro_PicBig = Pro_PicBig;
                }
                else
                {
                    model.Pro_Picture = model.Pro_Picture + "," + Pro_Picture;
                    model.Pro_PicSmall = model.Pro_PicSmall + "," + Pro_PicSmall;
                    model.Pro_PicBig = model.Pro_PicBig + "," + Pro_PicBig;
                }
            }
            if (bll.Update(model))
            {
                GetPicture();
                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('更新成功！');", true);
            }
            else
            {
                GetPicture();
                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('更新失败');", true);
            }

        }
    }
}
